<?php

namespace app\api\validate\share;

use think\Validate;
use app\logic\user\UserLogic;

class InviteValidate extends Validate
{
    protected $failException = true;

    protected $rule = [
        'creator_user_id' => 'require|checkIsCreator', // 添加自定义规则
        'invite_code' => 'require',
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'getShareLink' => ['creator_user_id'],
        'bind' => ['invite_code'],
    ];

    public function __construct()
    {
        $this->field = [
            'creator_user_id' => __('creator user id'),
            'invite_code' => __('invite code')
        ];
        $this->message = array_merge($this->message, [
            'creator_user_id.require' => __('Please input creator user id'),
            'creator_user_id.checkIsCreator' => __('not the creator by share'),
            'invite_code.require' => __('Please input invite code')
        ]);
        parent::__construct();
    }

    /**
     * 自定义验证规则：检查是否是创作者
     * 
     * @param mixed $value 验证的值
     * @param mixed $rule 验证规则
     * @param array $data 数据
     * @return bool|string
     */
    protected function checkIsCreator($value, $rule, $data)
    {
        // 使用 UserLogic 检查用户是否是创作者
        if (!UserLogic::checkIsCreator($value)) {
            return false; // 验证失败
        }
        return true; // 验证成功
    }
}
